<script setup>
import { ref, onMounted } from 'vue'
import { category,addApi, deleteApi, queryByIdApi ,updateApi } from '@/api/classmanage'
import { ElMessage } from 'element-plus'
import Zujian from './zujian.vue'

const tableData = ref([])
const zujianref=ref(null)

const treeProps = ref({
  children: 'children',
  label: 'name'
})

const getcate = async () => {
  const result = await category()
  tableData.value = result
 zujianref.value. parentOptions = result
}
const add = () => {
 zujianref.value. dialogVisible = true
 zujianref.value. formData = {
   name: '',
  parentId: ''
 }

}


// 修改操作
const handleUpdate =async (row) => {
  zujianref.value. dialogVisible = true
   const res = await queryByIdApi(row.id)
  zujianref.value. formData = res.data
   if (zujianref.value.formData.value.parentId === 0) {
     formData.value.parentId = "0"
   }
}

// 删除操作
const handleDelete = async(row) => {
const res = await deleteApi(row.id)
getcate ()
 
}
const handleSuccess = () => {
  getcate () // 刷新数据
}


onMounted(() => {
  getcate()
})
</script>
<template>

  <div class="table-container">
    <el-button type="primary" @click="add">新增</el-button>
    <el-table class="table-box" :data="tableData" :tree-props="treeProps" row-key="id" default-expand-all :border="false"
       style="width: 900px">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="name" label="商品类别" width="calc(100% - 380px)" />
      <el-table-column label="操作" width="300" align="center">
        <template #default="scope">
          <el-button size="small" type="primary" icon="Edit" @click="handleUpdate(scope.row)">
            修改
          </el-button>
          <el-button size="small" type="danger" icon="Delete" @click="handleDelete(scope.row)"
            style="margin-left: 12px">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <br />

  <Zujian ref="zujianref" @success="handleSuccess"></Zujian>
  </div>
</template>



<style scoped>

</style>